<!DOCTYPE html>
<html xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<meta name="viewport" content="width=device-width,initial-scale=1">
	<meta name="description" content="A fully featured admin theme which can be used to build CRM, CMS, etc.">
	<meta name="author" content="Coderthemes">
	
	<!-- jQuery  -->
	<script src="/js/jquery3/jquery.min.js"></script>
	
	<!-- layui  -->
	<link rel="stylesheet" href="/js/layuiadmin-1.2.1/layui2.7.6/css/layui.css" media="all" />
	<script type="text/javascript" src="/js/layuiadmin-1.2.1/layui2.7.6/layui.js"></script>
	
	<!-- 打印控件 -->
	<script src="/js/printWorld/PrintWorld.js"></script>
</head>

<body>
	<form class="layui-form" id="id_form" method="post" action="">

		<a href="javascript:void(0);" class="layui-btn layui-btn-sm layui-btn-normal" onclick="doPrintGoodsAll()" style="margin: 5px;">打印全部商品标</a>
		<a href="javascript:void(0);" class="layui-btn layui-btn-sm layui-btn-normal" onclick="doPrintTMBAll()" style="margin: 5px;">打印全部透明标</a>
		<table id="id_table_fenxiang"></table>
		<script th:inline="javascript">
		layui.use(['table', 'dropdown'], function(){
			var dom = $('#id_table_fenxiang');
			layui.table.render({
				elem: '#' + dom.attr('id'),
				limit: 9999,
				id: dom.attr('id'),
				rowDrag: {
					done: function(obj) {}
				},
				totalRow: true,
				cols: [ [
						{field: 'code', title: '商品编码', align:'center'},
						{field: 'name', title: '商品名称', align:'center'},
						{field: 'asin', title: 'ASIN', align:'center'},
						{field: 'sku', title: 'SKU', align:'center'},
						{field: 'fnsku', title: 'FNSKU', align:'center'},
						{field: 'num', title: '计划发货数量', align:'center'},
					    {field: 'signCode', title: '透明标记编码', align:'center'},
					{field: 'ope', title: '操作', align:'center', width:380, templet:function(data){
							var returnStr = '<a href="javascript:void(0);" class="layui-btn layui-btn-sm layui-btn-normal" onclick="doPrintGoods(\'' + data.guid + '\',\'' + data.code3 + '\')">打印商品标A4</a>';
							returnStr += '<a href="javascript:void(0);" class="layui-btn layui-btn-sm layui-btn-normal" onclick="doPrintGoods2(\'' + data.guid + '\',\'' + data.code3 + '\')">打印商品标70_30</a>';
							for(var i = 0; i < data.fileToumingbiaoArr.length; i++) {
								returnStr += '<a href="javascript:void(0);" class="layui-btn layui-btn-sm layui-btn-normal" onclick="doPrintTMB(\'' + data.fileToumingbiaoArr[i].link + '\')">打印透明标-' + (i + 1) + '</a>';
							}
							return returnStr;
						}}
				] ],
				data: $.parseJSON(JSON.stringify([[${dto.rows}]])),
				done: function(){
				}
			});
		})
		</script>
	</form>
	<script>
	$(function () {
		layui.use(['form'], function(){
			var form = layui.form;
			
			// 表单提交，共用同一个
			form.on('submit(formSubmit)', function(data){
				var checkStatus = layui.table.checkStatus('id_table_fenxiang');
				var rows = checkStatus.data;
				if (rows.length <= 0) {
					layer.msg('请选择至少一条数据');
					return false;
				}
				window.parent.tableOfShowingPerson.addRows(rows);
				var index = parent.layer.getFrameIndex(window.name);
				parent.layer.close(index);
				return false;
			});
		});
	});
	
	function doPrintGoods(guid,code3) {

		var urlBegin = window.location.href.split("//")[0];
		var json = {};
		json.action = "print";	//打印指令动作。json.action = "preview"，则生成预览。
		json.template = urlBegin + "//" + window.location.host + "/js/printWorld/muban/goods_a4_27.fmx";	//模板URL，位于WEB服务器的一个模板文件。
		
		$.post('/storage/hwTailprogram/getGoodsPrintInfo',{guid: guid,code3:code3},function(result){
			if (result.code == 0){
				alert("！打印的份数是：" + result.data.num + "份，可在预览页面右下角设置每页显示最多10个");
				var datas = [];
				for(var i = 1; i < result.data.num + 1;) {
					var pageObj = {};
					if(i % 27 == 1) {
						pageObj = {};
					}
					for(var j = 1; j < 28; j++) {
						
						if(i + j > result.data.num + 1) {
							pageObj['barcode' + j] = '';
							pageObj['barcodetext' + j] = '';
							pageObj['nameEn' + j] = '';
							pageObj['new' + j] = '';
						} else {
							pageObj['barcode' + j] = result.data.imgBarcode;
							pageObj['barcodetext' + j] = result.data.imgBarcode;
							pageObj['nameEn' + j] = result.data.nameEn;
							pageObj['new' + j] = 'NEW';
						}
						
					}
					i = i + 27;
					datas.push(pageObj);
					
					
					
					//datas.push({
					//	"barcode": result.data.imgBarcode,
					//	"barcodetext": result.data.imgBarcode,
					//	"nameEn": result.data.nameEn
					//});
				}
				json.data = datas;
				json.action = "preview";
				var pw = GetPrintWorld();	//获取打天下对象
				pw.Act(json);
			}
		});
	}
	
	function doPrintGoods2(guid,code3) {

		var urlBegin = window.location.href.split("//")[0];
		var json = {};
		json.action = "print";	//打印指令动作。json.action = "preview"，则生成预览。
		json.template = urlBegin + "//" + window.location.host + "/js/printWorld/muban/goods_70_30.fmx";	//模板URL，位于WEB服务器的一个模板文件。
		
		$.post('/storage/hwTailprogram/getGoodsPrintInfo',{guid: guid,code3:code3},function(result){
			if (result.code == 0){
				alert("！打印的份数是：" + result.data.num + "份，可在预览页面右下角设置每页显示最多10个");
				var datas = [];
				for(var i = 0; i < result.data.num; i++) {
					datas.push({
						"barcode": result.data.imgBarcode,
						"barcodetext": result.data.imgBarcode,
						"nameEn": result.data.nameEn
					});
				}
				json.data = datas;
				json.action = "preview";
				var pw = GetPrintWorld();	//获取打天下对象
				pw.Act(json);
			}
		});
	}
	
	function doPrintGoodsAll() {
		var urlBegin = window.location.href.split("//")[0];
		var json = {};
		json.action = "print";	//打印指令动作。json.action = "preview"，则生成预览。
		json.template = urlBegin + "//" + window.location.host + "/js/printWorld/muban/goods_a4_27.fmx";	//模板URL，位于WEB服务器的一个模板文件。
		
		$.post('/storage/hwTailprogram/getGoodsPrintInfoAll',{code: '[(${dto.code})]'},function(result){
			if (result.code == 0){
				var rows = result.data;
				var datas = [];
				for(var i = 0; i < rows.length; i++) {
					var row = rows[i];
					for(var j = 0; j < row.num; j++) {
						datas.push({
							"barcode": row.imgBarcode,
							"barcodetext": row.imgBarcode,
							"nameEn": row.nameEn
						});
					}
					if(i != rows.length) {
						datas.push({
							"barcode": '',
							"barcodetext": '',
							"nameEn": ''
						});
					}
				}
				json.data = datas;
				json.action = "print";
				var pw = GetPrintWorld();	//获取打天下对象
				pw.Act(json);
			}
		});
	}
	
	function doPrintTMBAll() {
		var datas = layui.table.cache['id_table_fenxiang'];
		var pdfs = [];
		$.post('/storage/hwTailprogram/doPrintTMBAll',{code: '[(${dto.code})]'},function(result){
			if (result.code == 0){
				for(var i = 0; i < datas.length; i++) {
					var data = datas[i];
					var tmbs = data.fileToumingbiaoArr;
					for(var j = 0; j < tmbs.length; j++) {
						pdfs.push(tmbs[j].link);
					}

					// 控制中间用空白相隔
					if(tmbs.length > 0 && i != datas.length) {
						pdfs.push('https://file.mini-ease.com/upload/static/tmb_blank.pdf');
					}
				}

				var json = {};
				json.action = "previewfile";	//打印指令动作。json.action = "preview"，则生成预览。
				json.format = "pdf_url";        //指定格式为pdf_url。 //指定格式为pdf_url。

				json.docwidth = 5; // 单位：cm
				json.doclength = 2.2; // 单位：cm
				json.paperwidth = 5;
				json.paperlength = 3;

				json.content = pdfs;   //pdf文件的url路径
				json.cert = window.location.href.split("//")[0] + "//" + window.location.host + "/js/printWorld/pdf_mini-ease.com.ctf";
				var pw = GetPrintWorld();    //获取打天下对象
				pw.Act(json);
			}else {
				layer.msg(result.msg);
			}
		})

		return;
	}
	
	function doPrintTMB(urlPdf) {
		$.post('/storage/hwTailprogram/doPrintTMB',{code: '[(${dto.code})]'},function(result) {
			if (result.code == 0){
				var json = {};
				json.action = "previewfile";	//打印指令动作。json.action = "preview"，则生成预览。
				json.format = "pdf_url";        //指定格式为pdf_url。 //指定格式为pdf_url。

				json.docwidth = 5; // 单位：cm
				json.doclength = 2.2; // 单位：cm
				json.paperwidth = 5;
				json.paperlength = 3;
				//json.margin3 = 0.5;
				//json.papersize = 1;

				json.content = urlPdf;   //pdf文件的url路径
				json.cert = window.location.href.split("//")[0] + "//" + window.location.host + "/js/printWorld/pdf_mini-ease.com.ctf";
				var pw = GetPrintWorld();    //获取打天下对象
				pw.Act(json);
			}else {
				layer.msg(result.msg);
			}
		})
	}
	</script>
</body>
</html>